package com.rs2hd.combat;

import com.rs2hd.event.Event;
import com.rs2hd.model.Player;
import com.rs2hd.model.World;
import com.rs2hd.util.Misc;

public class Magic {
	private Player p;
	public int projectile = -1;
	public int req;
	private int gfx2;
	private int damage;
	private int anim;
	public int gfx;
	public boolean usingAncient;
	private int magicIndex;
	boolean freeze = false;
	boolean barrage = false;
	int freezeDelay = 0;
	public Magic(Player p) {
		this.p = p;
	}
	public void handleMagic(int playerId, int interfaceId, int spellId) {
		if(p == null || p.isDead()) {
			return;
		}
		if(p.getCombat().combatDelay > 0) {
			return;
		}
		magicIndex = playerId;
		final Player opp = World.getInstance().getPlayerList().get(playerId);
		p.getCombat().combatDelay = 4;
		p.turnTemporarilyTo(opp);
		if (p.SafeZone()) {
			p.getActionSender().sendMessage("You are not in a PvP area to be allowed to attack!");
			p.getCombat().attacking = false;
			return;
		}
		if (opp.SafeZone()) {
			p.getActionSender().sendMessage("This player is not in a PvP area to be attacked!");
			p.getCombat().attacking = false;
			return;
		}
		 if (p.getCombat().inCombatWith != opp.getIndex() && p.getCombat().inCombatWith != 0) {	
		  if (opp.NormalZone()) {			
			p.getActionSender().sendMessage("You are already in combat.");	
			p.getCombat().attacking = false;
			return;
                   }
                 }
		 if (opp.getCombat().inCombatWith != p.getIndex() && opp.getCombat().inCombatWith != 0 ) {
		  if (p.NormalZone()) {				
			p.getActionSender().sendMessage("That player is already in combat.");	
			p.getCombat().attacking = false;
			return;
                  }
                }
		p.getCombat().inCombatWith = opp.getIndex();
		p.getCombat().combatWithTime = 8;
		opp.getCombat().inCombatWith = p.getIndex();
		opp.getCombat().combatWithTime = 8;
		switch(interfaceId) {
		case 192:
			switch(spellId) {
			case 25://Wind strike
				gfx = 90;
				anim = 1162;
				projectile = 91;
				damage = 2;
				gfx2 = 92;
				req = 1;
				usingAncient = false;
				break;
			case 28: //Water strike
				gfx = 93;
				anim = 1162;
				projectile = 94;
				damage = 4;
				gfx2 = 95;
				req = 5;
				usingAncient = false;
				break;
			case 30: //Earth strike
				gfx = 96;
				anim = 1162;
				projectile = 97;
				damage = 6;
				gfx2 = 98;
				req = 9;
				usingAncient = false;
				break;
			case 32: //Fire strike
				gfx = 99;
				anim = 1162;
				projectile = 100;
				damage = 8;
				gfx2 = 101;
				req = 13;
				usingAncient = false;
				break;
			case 34: //Wind bolt
				gfx = 117;
				anim = 1162;
				projectile = 118;
				damage = 9;
				gfx2 = 119;
				req = 17;
				usingAncient = false;
				break;
			case 39: //Water bolt
				gfx = 120;
				anim = 1162;
				projectile = 121;
				damage = 10;
				gfx2 = 122;
				req = 23;
				usingAncient = false;
				break;
			case 42: //Earth bolt
				gfx = 123;
				anim = 1162;
				projectile = 124;
				damage = 11;
				gfx2 = 125;
				req = 29;
				usingAncient = false;
				break;
			case 45: //Fire bolt
				gfx = 126;
				anim = 1162;
				projectile = 127;
				damage = 12;
				gfx2 = 128;
				req = 35;
				usingAncient = false;
				break;
			case 49: //Wind blast
				gfx = 132;
				anim = 1162;
				projectile = 133;
				damage = 13;
				gfx2 = 134;
				req = 41;
				usingAncient = false;
				break;
			case 52: //Water blast
				gfx = 135;
				anim = 1162;
				projectile = 136;
				damage = 14;
				gfx2 = 137;
				req = 47;
				usingAncient = false;
				break;
			case 58: //Earth blast
				gfx = 138;
				anim = 1162;
				projectile = 139;
				damage = 15;
				gfx2 = 140;
				req = 53;
				usingAncient = false;
				break;
			case 38: //Fire blast
				gfx = 129;
				anim = 1162;
				projectile = 130;
				damage = 16;
				gfx2 = 131;
				req = 59;
				usingAncient = false;
				break;
			case 70: //Wind wave
				gfx = 158;
				anim = 1162;
				projectile = 159;
				damage = 17;
				gfx2 = 160;
				req = 62;
				usingAncient = false;
				break;
			case 73: //Water wave
				gfx = 161;
				anim = 1162;
				projectile = 162;
				damage = 18;
				gfx2 = 163;
				req = 65;
				usingAncient = false;
				break;
			case 77: //Earth Wave
				gfx = 164;
				anim = 1162;
				projectile = 165;
				damage = 19;
				gfx2 = 166;
				req = 70;
				usingAncient = false;
				break;
			case 80: //FireWave
				gfx = 155;
				anim = 1162;
				projectile = 156;
				damage = 20;
				gfx2 = 157;
				req = 75;
				usingAncient = false;
				break;	
			}
			break;
		case 193:
			projectile = -1;
			gfx = -1;
			usingAncient = true;
			switch (spellId) {
			case 23: //Ice barrage
				gfx = 368;
				anim = 1979;
				damage = 30;
				gfx2 = 369;
				req = 94;
				freeze = true;
				freezeDelay = 20;
				break;	
			case 35: //Shadow barrage
				//gfx = 368;
				anim = 1978;
				damage = 28;
				gfx2 = 383;
				req = 88;
				break;
			case 31: //Smoke barrage
				//gfx = 368;
				anim = 1979;
				damage = 27;
				gfx2 = 391;
				req = 86;
				break;	
			case 21: //Ice blitz
				//gfx = 368;
				anim = 1978;
				damage = 26;
				gfx2 = 367;
				req = 82;
				freeze = true;
				freezeDelay = 15;
				break;	
			case 25: //Blood Blitz
				//gfx = 368;
				anim = 1978;
				damage = 25;
				gfx2 = 375;
				req = 80;
				break;	
			case 33: //Shadow blitz
				//gfx = 368;
				anim = 1978;
				damage = 24;
				gfx2 = 381;
				req = 76;
				break;	
			case 29: //Smoke blitz
				//gfx = 368;
				anim = 1978;
				damage = 23;
				gfx2 = 387;
				req = 74;
				break;	
			case 22: //Ice burst
				// gfx = 368;
				anim = 1979;
				damage = 22;
				gfx2 = 363;
				req = 70;
				freeze = true;
				freezeDelay = 10;
				break;	
			case 26: //Blood Burst
				// gfx = 368;
				anim = 1979;
				damage = 21;
				gfx2 = 376;
				req = 68;
				break;
			case 34: //Shadow Burst
				//gfx = 368;
				anim = 1978;
				damage = 17;
				gfx2 = 382;
				req = 64;
				break;	
			case 30: //Smoke Burst
				//gfx = 368;
				anim = 1979;
				damage = 17;
				gfx2 = 389;
				req = 62;
				break;	
			case 20: //Ice rush
				//gfx = 368;
				anim = 1978;
				damage = 16;
				gfx2 = 361;
				req = 58;
				freeze = true;
				freezeDelay = 5;
				break;	
			case 24: //Blood Rush
				//gfx = 368;
				anim = 1978;
				damage = 15;
				gfx2 = 361;
				req = 56;
				break;	
			case 32: //Shadow Rush
				//gfx = 368;
				anim = 1978;
				damage = 14;
				gfx2 = 379;
				req = 52;
				break;
			case 39: //Shadow Rush
				//gfx = 368;
				anim = 10518;
				damage = 38;
				gfx2 = 1853;
				req = 52;
				usingAncient = false;
				break;
			case 37: //Shadow Rush
				//gfx = 368;
				anim = 10524;
				damage = 30;
				gfx2 = 1850;
				req = 52;
				usingAncient = false;
				break;
			case 38: //Shadow Rush
				//gfx = 368;
				anim = 10516;
				damage = 25;
				gfx2 = 1848;
				req = 52;
				usingAncient = false;
				break;
			case 36: //Shadow Rush
				//gfx = 368;
				anim = 10513;
				damage = 15;
				gfx2 = 1845;
				req = 52;
				usingAncient = false;
				break;
			case 28: //Smoke Rush
				// gfx = 368;
				anim = 1978;
				damage = 13;
				gfx2 = 385;
				req = 50;
				break;	
			}
			break;
		}
		if ( p.getSkills().getLevelForXp(6) <= req) {
			p.getActionSender().sendMessage("You need level " + req+ " magic to use this spell.");
		} else {
			p.animate(anim);
			if (barrage) {	
				p.graphics(gfx);
			} else { 
				p.graphics(gfx, (100 << 16));
			}
			//World.getInstance().getProjectileManager().fire(p.getLocation(), opp.getLocation(),  50, 100, getProjectile(), 46, 31, opp);
			World.getInstance().registerEvent(new Event(getMagicDelay()) {
				
				@Override
				public void execute() {
					int finalDamage = Misc.random(damage);
					opp.updateHit(p, finalDamage);
					opp.hit(finalDamage);
					if (!usingAncient) {
						opp.graphics(gfx2,  (100 << 16));
					} else {
						if (freeze) {
							if (opp.frozen == 0) {
								opp.frozen = freezeDelay;
								opp.graphics(369);
								opp.getWalkingQueue().reset();
							} else {
								opp.graphics(1677, (100 << 16));
								reset();
								this.stop();
								return;
							}
						}
						opp.graphics(gfx2);
					}
					reset();
					this.stop();
				}
			});
		}
	}
	public void reset() {
		gfx = -1;
		anim = -1;
		projectile = -1;
		gfx2 = -1;
		damage = -1;
		req = -1;
	}
	public int getProjectile() {
		return projectile;
	}
	public int getMagicDelay() {
		Player p2 = World.getInstance().getPlayerList().get(magicIndex);
		if (p == null || p2 == null)
			return 1500;
		if (Misc.getDistance(p.getLocation().getX(), p.getLocation().getY(), p2.getLocation().getX(), p2.getLocation().getY()) == 1)
			return 755;
		if (Misc.getDistance(p.getLocation().getX(), p.getLocation().getY(), p2.getLocation().getX(), p2.getLocation().getY()) == 2)
			return 755;
		if (Misc.getDistance(p.getLocation().getX(), p.getLocation().getY(), p2.getLocation().getX(), p2.getLocation().getY()) == 3)
			return 1000;
		if (Misc.getDistance(p.getLocation().getX(), p.getLocation().getY(), p2.getLocation().getX(), p2.getLocation().getY()) == 4)
			return 1250;
		if (Misc.getDistance(p.getLocation().getX(), p.getLocation().getY(), p2.getLocation().getX(), p2.getLocation().getY()) == 5)
			return 1250;
		if (Misc.getDistance(p.getLocation().getX(), p.getLocation().getY(), p2.getLocation().getX(), p2.getLocation().getY()) == 6)
			return 1250;
		if (Misc.getDistance(p.getLocation().getX(), p.getLocation().getY(), p2.getLocation().getX(), p2.getLocation().getY()) == 7)
			return 1250;
		if (Misc.getDistance(p.getLocation().getX(), p.getLocation().getY(), p2.getLocation().getX(), p2.getLocation().getY()) == 8)
			return 1250;
		if (Misc.getDistance(p.getLocation().getX(), p.getLocation().getY(), p2.getLocation().getX(), p2.getLocation().getY()) == 9)
			return 1245;
		if (Misc.getDistance(p.getLocation().getX(), p.getLocation().getY(), p2.getLocation().getX(), p2.getLocation().getY()) == 10)
			return 1245;
		return 1;
	}
}
